# 编译器和工具链配置
# CC := riscv64-unknown-linux-gnu-gcc
# CXX := riscv64-unknown-linux-gnu-g++
# OBJCOPY := riscv64-unknown-linux-gnu-objcopy
# OBJDUMP := riscv64-unknown-linux-gnu-objdump
# SIZE := riscv64-unknown-linux-gnu-size

CC := riscv64-unknown-elf-gcc
CXX := riscv64-unknown-elf-g++
OBJCOPY := riscv64-unknown-elf-objcopy
OBJDUMP := riscv64-unknown-elf-objdump
SIZE := riscv64-unknown-elf-size


OBJ := obj
FREERTOS_DIR := FreeRTOS/Source
APP_DIR      := FreeRTOS/App
STARTUP_DIR  := startup
BOOT_DIR 	 := 00_boot0
FinalName := my_freertos
Linkfile := link.ld

# 搜索源文件
C_SOURCES   := $(wildcard $(BOOT_DIR)/*.c) \
				$(wildcard $(FREERTOS_DIR)/*.c) \
               $(wildcard $(FREERTOS_DIR)/portable/GCC/RISC-V/*.c) \
               $(wildcard $(FREERTOS_DIR)/portable/MemMang/*.c) \
               $(wildcard $(APP_DIR)/*.c) 

S_SOURCES   := $(wildcard $(FREERTOS_DIR)/portable/GCC/RISC-V/*.S) \
               $(wildcard $(STARTUP_DIR)/*.S) \
               $(wildcard $(BOOT_DIR)/*.S)

# 架构和编译选项
MCFLAGS := -march=rv64gc -mabi=lp64d -mcmodel=medlow -fno-stack-protector -D__riscv_xlen=64 -D__riscv64 -DportasmHANDLE_INTERRUPT=handle_trap
INCLUDES := -I./00_boot0/include \
            -I./FreeRTOS/Source/portable/GCC/RISC-V \
            -I./FreeRTOS/Source/include \
            -I./FreeRTOS/Source/portable/GCC/RISC-V/chip_specific_extensions/RISCV_MTIME_CLINT_no_extensions \
            -I./FreeRTOS/App 

CFLAGS := -g -ggdb -W -Wall -O3 $(MCFLAGS) $(INCLUDES) 
CXXFLAGS := $(CFLAGS)
ASFLAGS := $(MCFLAGS) $(INCLUDES) -g -ggdb -Wall
LDFLAGS := -T $(Linkfile) -nostartfiles -lc -lgcc
# LDFLAGS := -T link.ld  -nostartfiles -Wl,--gc-sections


# 构建对象文件列表
COPJS := $(addprefix $(OBJ)/,$(notdir $(C_SOURCES:.c=.o)))
SOBJS := $(addprefix $(OBJ)/,$(notdir $(S_SOURCES:.S=.o)))
OBJ_COLLECTION := $(COPJS) $(SOBJS)

.PHONY: all clean

debug:
	@echo "C_SOURCES: $(C_SOURCES)"
	@echo "S_SOURCES: $(S_SOURCES)"
	@echo "COPJS: $(COPJS)"
	@echo "SOBJS: $(SOBJS)"
	@echo "OBJ_COLLECTION: $(OBJ_COLLECTION)"

all: $(FinalName).bin
	@echo "make: 编译完成, 已生成$(FinalName).bin"

$(OBJ):
	mkdir -p $(OBJ)

# 针对 C 文件的编译规则
$(OBJ)/%.o: $(FREERTOS_DIR)/%.c | $(OBJ)
	$(CC) $(CFLAGS) -MD -MP -MF $@.d -c $< -o $@

$(OBJ)/%.o: $(FREERTOS_DIR)/portable/GCC/RISC-V/%.c | $(OBJ)
	$(CC) $(CFLAGS) -MD -MP -MF $@.d -c $< -o $@

$(OBJ)/%.o: $(FREERTOS_DIR)/portable/MemMang/%.c | $(OBJ)
	$(CC) $(CFLAGS) -MD -MP -MF $@.d -c $< -o $@

$(OBJ)/%.o: $(APP_DIR)/%.c | $(OBJ)
	$(CC) $(CFLAGS) -MD -MP -MF $@.d -c $< -o $@

$(OBJ)/%.o: $(BOOT_DIR)/%.c | $(OBJ)
	$(CC) $(CFLAGS) -MD -MP -MF $@.d -c $< -o $@

# 针对汇编文件的编译规则
$(OBJ)/%.o: $(FREERTOS_DIR)/portable/GCC/RISC-V/%.S | $(OBJ)
	$(CC) $(ASFLAGS) -MD -MP -MF $@.d -c $< -o $@

$(OBJ)/%.o: $(STARTUP_DIR)/%.S | $(OBJ)
	$(CC) $(ASFLAGS) -MD -MP -MF $@.d -c $< -o $@

$(OBJ)/%.o: $(BOOT_DIR)/%.S | $(OBJ)
	$(CC) $(ASFLAGS) -MD -MP -MF $@.d -c $< -o $@

# 链接生成ELF
$(FinalName).elf: $(OBJ_COLLECTION)
	$(CC) $(LDFLAGS) -Wl,--cref,-Map=$(FinalName).map $^ -o $@ -lgcc
	$(OBJDUMP) -D $@ > $(FinalName).lst
	$(SIZE) $@
	@echo "$(FinalName).elf 构建完成"

# 从ELF生成BIN
$(FinalName).bin: $(FinalName).elf
	$(OBJCOPY) -v -O binary $(FinalName).elf $(FinalName).bin
	@echo "已生成 $(FinalName).bin"
	@echo "为 brom 启动生成头信息"
	@./00_boot0/tools/windows/mksunxi $(FinalName).bin

clean:
	rm -rf $(FinalName).elf $(FinalName).hex $(FinalName).map $(FinalName).lst $(FinalName).bin $(OBJ)/*.o $(OBJ)/*.d
	@echo "make: 清理完成"
